package com.itheima.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.Md5Utils;
import com.itheima.common.R;

import com.itheima.entity.Employee;
import com.itheima.service.Service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.Session;

import java.time.LocalDateTime;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R Login(HttpServletRequest request, @RequestBody Employee emp){
        //设置查询的数据与数据库数据的关系，查询username的数据数据库中是否有这个数据
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        QueryWrapper<Employee> username = wrapper.eq("username", emp.getUsername());
        Employee byId = employeeService.getOne(username);
        if(byId==null){
            return R.error("此用户不存在");
        }
        if (byId.getStatus()!=1){
            return R.error("该用户被禁用");
        }
        //查出有这个用户后判断密码是否正确
        //通过工具类将密码转换成MD5格式
        String MD5Str = Md5Utils.md5(emp.getPassword()).toLowerCase();
        if(!byId.getPassword().equals(MD5Str)){
            return R.error("密码错误");
        }
        request.getSession().setAttribute("employee",byId.getId());
        return R.success(byId);

    }

    @PostMapping("logout")
    public R LogOut(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");

    }
    @PostMapping()
    public R save(HttpServletRequest req,@RequestBody Employee employee){
        //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置创建修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //更新创建及修改人
        Long id=(Long)req.getSession().getAttribute("employee");
        employee.setCreateUser(id);
        employee.setUpdateUser(id);
        employeeService.save(employee);
        return R.success("新增员工成功");
    }
    @GetMapping("/page")
    public R page( HttpServletRequest request,int page, int pageSize, String name){
        //分页构造器
        Page pageInfo=new Page(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        //过滤条件
        lqw.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lqw.ne(Employee::getId,request.getSession().getAttribute("employee"));
        //排序条件
        lqw.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo,lqw);
        return R.success(pageInfo);
    }
    @PutMapping()
    public R updateSt(HttpServletRequest request,@RequestBody Employee employee){
//       QueryWrapper<Employee> lqw=new QueryWrap per<>();
//        lqw.eq("id",employee.getId());
        //修改员工数据 update_time
        Long id=(Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(id);
//        employee.setStatus(0);
        employeeService.updateById(employee);
        return R.success("权限修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }

}
